package com.yashi.springmvcjbpm.dao.impl;


import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;


import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
import com.yashi.springmvcjbpm.dao.StaffDAO;
import com.yashi.springmvcjbpm.util.GetConnection;

@Repository
public class StaffDAOImpl implements StaffDAO {
	GetConnection connection = new GetConnection();
	Statement st =null;
	Connection  con  =null;
	
	public List findStaff(String queryString) {
		System.out.println("sql="+queryString);
		List list = new ArrayList();
		 try {
			 con=connection.getCon();
			 st = (Statement) con.createStatement();
			ResultSet rs =st.executeQuery(queryString);
			list = resultSetToList(rs);
			} catch (Exception e) {			
				e.printStackTrace();
			}    
		return list;
	}

	public static List resultSetToList(ResultSet rs) throws java.sql.SQLException {   
        if (rs == null)   
            return Collections.EMPTY_LIST;   
        ResultSetMetaData md = rs.getMetaData(); //得到结果�?(rs)的结构信息，比如字段数�?�字段名�?   
        int columnCount = md.getColumnCount(); //返回�? ResultSet 对象中的列数   
        List list = new ArrayList();   
        Map rowData = new HashMap();   
        while (rs.next()) {   
         rowData = new HashMap(columnCount);   
         for (int i = 1; i <= columnCount; i++) {
        	 if(md.getColumnName(i).equals("leaveLong")) {
        		 rowData.put(md.getColumnName(i), rs.getObject(i));  
//        		 rowData.put(md.getColumnName(i), rs.getObject(i)+"天"); 
        	 }else {
        		 rowData.put(md.getColumnName(i), rs.getObject(i));           		 
        	 }
         }   
         list.add(rowData);   
        }   
        return list;   
	} 
	
	public String getStaffPosition(String staffName) {
		String queryString = "select * from `staff` where staffName='"+staffName+"'";
		System.out.println("sql="+queryString);
		List list = new ArrayList();
		String staffPosition = null;
		 try {
			 con=connection.getCon();
			 st = (Statement) con.createStatement();
			ResultSet rs =st.executeQuery(queryString);
			list = resultSetToList(rs);
			Map map = (HashMap)list.get(0);
			staffPosition = (String)map.get("staffPosition");
			} catch (Exception e) {			
				e.printStackTrace();
			}    
		return staffPosition;
	}

	public boolean updateStaff(String sql) {
		boolean flag = false ;
		try {
			 con=connection.getCon();
			 st = (Statement) con.createStatement();
			 st.execute(sql);
			 flag =true;
			} catch (Exception e) {			
				e.printStackTrace();
				flag = false;
			}
		return flag;  
		
	}
	
}
